package com.aleaho.cftrack.presenter;

import android.app.Activity;

import com.aleaho.cftrack.CFTrackApplication;
import com.aleaho.cftrack.R;
import com.aleaho.cftrack.activity.TrackQueryActivity;
import com.aleaho.cftrack.model.TrackQueryModel;
import com.aleaho.cftrack.model.TrackQueryModelImpl;
import com.aleaho.cftrack.utils.CommonUtil;
import com.aleaho.cftrack.utils.Constants;
import com.aleaho.cftrack.utils.MapUtil;
import com.aleaho.cftrack.utils.TrackConsts;
import com.aleaho.cftrack.utils.ViewUtil;
import com.aleaho.cftrack.view.TrackQueryView;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.model.LatLng;
import com.baidu.trace.api.analysis.DrivingBehaviorRequest;
import com.baidu.trace.api.analysis.DrivingBehaviorResponse;
import com.baidu.trace.api.analysis.OnAnalysisListener;
import com.baidu.trace.api.analysis.StayPointRequest;
import com.baidu.trace.api.analysis.StayPointResponse;
import com.baidu.trace.api.track.DistanceResponse;
import com.baidu.trace.api.track.HistoryTrackRequest;
import com.baidu.trace.api.track.HistoryTrackResponse;
import com.baidu.trace.api.track.LatestPointResponse;
import com.baidu.trace.api.track.OnTrackListener;
import com.baidu.trace.api.track.TrackPoint;
import com.baidu.trace.model.Point;
import com.baidu.trace.model.StatusCodes;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Aleaho
 * @version 1.0
 * @date 2017/11/19
 * @desc
 */

public class TrackQueryPresenter extends OnTrackListener implements OnAnalysisListener {


    List<LatLng> trackPoints = null;
    List<List<LatLng>> multipleTrackPoints = null;
    List<LatLng> tempPoints = null;

    private CFTrackApplication trackApp = null;
    private TrackQueryView trackQueryView = null;
    private Activity activity = null;
    private TrackQueryModel trackQueryModel = null;
    private ViewUtil viewUtil = null;
    private int pageIndex = 1;

    public TrackQueryPresenter(CFTrackApplication trackApp,
                               TrackQueryView trackQueryView,
                               Activity activity) {

        this.trackApp = trackApp;
        this.trackQueryView = trackQueryView;
        this.activity = activity;
        this.trackQueryModel = new TrackQueryModelImpl();
        viewUtil = new ViewUtil();

    }


    /**
     * 查询历史轨迹
     *
     * @param historyTrackRequest 历史轨迹查询条件
     */
    public void queryHistoryTrack(HistoryTrackRequest historyTrackRequest) {

        trackQueryModel.queryHistoryTrack(this.trackApp, historyTrackRequest, this);
    }


    /**
     * 查询驾驶行为
     *
     * @param drivingBehaviorRequest 驾驶行为查询条件
     */
    public void queryDrivingBehavior(DrivingBehaviorRequest drivingBehaviorRequest) {
        trackQueryModel.queryDrivingBehavior(this.trackApp, drivingBehaviorRequest, this);

    }


    /**
     * 查询停留点
     *
     * @param stayPointRequest
     */
    public void queryStayPoint(StayPointRequest stayPointRequest) {
        trackQueryModel.queryStayPoint(this.trackApp, stayPointRequest, this);

    }


    @Override
    public void onLatestPointCallback(LatestPointResponse latestPointResponse) {
        super.onLatestPointCallback(latestPointResponse);
    }

    @Override
    public void onDistanceCallback(DistanceResponse distanceResponse) {
        super.onDistanceCallback(distanceResponse);
    }


    @Override
    public void onHistoryTrackCallback(HistoryTrackResponse response) {
        int total = response.getTotal();

        if (StatusCodes.SUCCESS != response.getStatus()) {
            trackQueryView.clearMapWhenNoTrack();
            viewUtil.showToast(activity, response.getMessage());
        } else if (0 == total) {
            trackQueryView.clearMapWhenNoTrack();
            viewUtil.showToast(activity, activity.getResources().getString(R.string.no_track_data));
        } else {

            List<TrackPoint> points = response.getTrackPoints();
            if (null != points) {
                if (trackPoints != null) {
                    trackPoints.clear();
                } else if (multipleTrackPoints != null) {
                    multipleTrackPoints.clear();
                } else {
                    trackPoints = new ArrayList<>();
                    multipleTrackPoints = new ArrayList<>();
                }

                TrackPoint lastPoint = null;
                for (TrackPoint trackPoint : points) {

                    if (lastPoint == null) {
                        lastPoint = trackPoint;
                    }
                    if (!CommonUtil.isZeroPoint(trackPoint.getLocation().getLatitude(),
                            trackPoint.getLocation().getLongitude())) {
                        trackPoints.add(MapUtil.convertTrace2Map(trackPoint.getLocation()));
                    }
                    if (lastPoint.getLocTime() / 1000 - trackPoint.getLocTime() / 1000 > TrackConsts.UNLINK_TIME) {

                        multipleTrackPoints.add(trackPoints);
                        trackPoints.clear();
                    }

                    lastPoint = trackPoint;
                }
                if(multipleTrackPoints.isEmpty()){
                    multipleTrackPoints.add(trackPoints);
                }

                trackQueryView.drawHistoryTrack(multipleTrackPoints, total);
            }
        }
    }

    @Override
    public void onStayPointCallback(StayPointResponse response) {
        if (StatusCodes.SUCCESS != response.getStatus()) {
            trackQueryView.setLastQueryTimeToZero();
            viewUtil.showToast(activity, response.getMessage());
            return;
        }
        if (0 == response.getStayPointNum()) {
            return;
        }

        trackQueryView.handleStayPoint(response);
    }

    @Override
    public void onDrivingBehaviorCallback(DrivingBehaviorResponse response) {

        if (StatusCodes.SUCCESS != response.getStatus()) {
            trackQueryView.setLastQueryTimeToZero();
            viewUtil.showToast(this.activity, response.getMessage());
            return;
        }

        if (0 == response.getSpeedingNum() && 0 == response.getHarshAccelerationNum()
                && 0 == response.getHarshBreakingNum() && 0 == response.getHarshSteeringNum()) {
            return;
        }

        trackQueryView.handleDrivingBehavior(response);

    }
}
